package com.mall.product.controller;

import java.util.*;


import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrGroupService;
import com.mall.product.service.CategoryService;
import com.mall.product.vo.AttrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;



/**
 * 商品属性
 *
 * @author xzt
 * @email xzt@163.com
 * @date 2024-08-10 15:54:08
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService ;

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;



    @RequestMapping("/{attrType}/list/{categoryId}")
    public R baseList(@RequestParam Map<String, Object> params ,@PathVariable("attrType")String attrType,@PathVariable("categoryId") Long categoryId){

        PageUtils page = attrService.queryPage(params,categoryId,attrType);
        List<AttrVO> attrVOList = new ArrayList<>();
        //添加所属分类和所属分组
        List<AttrEntity> attrList = (List<AttrEntity>) page.getList();

        attrList.forEach(attr ->{
            AttrVO attrVO = new AttrVO();
            BeanUtils.copyProperties(attr,attrVO);
            attrVOList.add(attrVO);
        });

        page.setList(attrVOList);
        attrVOList.forEach(item -> {
            //查询出分类
           CategoryEntity category = categoryService.getById(item.getCatelogId());
           item.setCatelogName(category.getName());

           if(item.getAttrType() == 1 ){
               AttrGroupEntity attrGroup= attrgroupRelationService.getAttrGroupNameByAttrId(item.getAttrId());
              if(!Objects.isNull(attrGroup)){
                  item.setGroupName(attrGroup.getAttrGroupName());
              }

           }

        });

        return R.ok().put("page", page);
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrVO attrVO = attrService.getAttrVO(attrId);
        return R.ok().put("attr", attrVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attrVO){
		attrService.saveAttrRelation(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
